﻿
//{{{ file documentation -------------------------------------------------------

/*!
 * @file     main.cpp
 * @brief    dicodor main function
 */

//}}} --------------------------------------------------------------------------
//{{{ includes / predeclarations -----------------------------------------------

// @(#) $Id: main.cpp 53 2012-05-23 13:56:41Z nsouchon-dico@altern.org $

#include "config.h"
#include "mainwindow.h"

#include <QtCore/QDebug>
#include <QtCore/QDir>
#include <QtCore/QTranslator>
#include <QtCore/QLibraryInfo>
#include <QtCore/QTextCodec>
#include <QtCore/QLocale>

#include <QtGui/QApplication>
#include <QtGui/QSplashScreen>
#include <QtGui/QTextDocumentWriter>

#include <ctime>

//}}} --------------------------------------------------------------------------
//{{{ void test () -------------------------------------------------------------

void test ()
{
    // same comment as in initDico ()

    if (false)
    {
        qDebug () << QIcon::themeSearchPaths () << QIcon::themeName ();
    }
}

//}}} --------------------------------------------------------------------------
//{{{ void initDico () ---------------------------------------------------------

void initDico ()
{
    // as somebody said: a souchon's way of life
    // assume 
    //    optimization will prevent including this part of code in executable
    //    but compiler will oblige us to keep it compilable
    //    reducing problems reintroducing it

    if (false)
    {
        // **** aspell dump master it | sort > /home/gilles/Bureau/dico_italien.txt

        QFile file ("/home/gilles/Bureau/dico_italien.txt");
        file.open (QIODevice::ReadOnly);
        QString contain (file.readAll ());
        file.close ();
        QStringList list (contain.split ("\n", QString::SkipEmptyParts));
        QStringList result;
        foreach (const QString & word, list)
        {
            QString wd (word);
            int pos = word.indexOf ('/');
            if (pos)
            {
                wd.truncate (pos);
            }
            if (wd.length () > 1)
            {
                result << wd;
            }
        }
        QByteArray data (result.join ("\n").toLatin1 ());
        QFile file2 ("/home/gilles/Bureau/dic_it.txt");
        file2.open (QIODevice::WriteOnly);
        file2.write (data);
        file2.close ();
    }
}

//}}} --------------------------------------------------------------------------
//{{{ bool initDirs () ---------------------------------------------------------

bool initDirs ()
{
    static QString error (QObject::tr ("Impossible de créer le répertoire %1\nArrêt de l'application."));
    bool           ok = true;
    const QDir     dir;

    if (! dir.mkpath (Config::appWorkPath ()))
    {
        qDebug () << error.arg (Config::appWorkPath ());
        ok = false;
    }

    if (! dir.mkpath (Config::appHomePath ()))
    {
        qDebug () << error.arg (Config::appHomePath ());
        ok = false;
    }

    if (! dir.mkpath (Config::appExportPath ()))
    {
        qDebug () << error.arg (Config::appExportPath ());
        ok = false;
    }

    return ok;
}

//}}} --------------------------------------------------------------------------
//{{{ int main (int argc, char * argv[]) ---------------------------------------

int main (int argc, char * argv[])
{
    // ****************************
    // * Initialisations importantes *
    // ****************************

    qsrand (time (NULL)); // semence aléatoire

    QApplication::setDesktopSettingsAware (true);
    QApplication::setQuitOnLastWindowClosed (true);
    QApplication::setGraphicsSystem ("raster");

    QApplication app (argc, argv);

    app.setOrganizationName ("PapaJaac");
    app.setApplicationName ("dicodor");
    app.setApplicationVersion (Config::appVersion ());
    app.setWindowIcon (Config::appIcon ());

    // dear users, be patient

    QSplashScreen splash (QPixmap (":/images/splash.png"));
    splash.show ();
    app.processEvents ();

    // some codecs

    QTextCodec::setCodecForTr (QTextCodec::codecForName ("UTF-8"));
    QTextCodec::setCodecForCStrings (QTextCodec::codecForName ("UTF-8"));
    QTextCodec::setCodecForLocale (QTextCodec::codecForName ("UTF-8"));

    // pour avoir les boutons des boîtes de dialogue dans la langue locale (fr par défaut)

    QTranslator qtTranslator;
    qtTranslator.load(
                "qt_" + QLocale::system ().name (),
                QLibraryInfo::location (QLibraryInfo::TranslationsPath)
                );
    app.installTranslator (&qtTranslator);

    // pour avoir l'appli localisée

    QTranslator appTranslator;
    appTranslator.load(
                Config::appName () + '_' + QLocale::system ().name (),
                ":/i18n/"
                );
    app.installTranslator (&appTranslator);

    // display version

    splash.showMessage (QObject::tr ("%1 version %2\n")
                        .arg (Config::appName ())
                        .arg (Config::appVersion ()),
                        Qt::AlignBottom | Qt::AlignHCenter,
                        Qt::blue);
    app.processEvents ();

    // init dirs and dico, test

    if (! initDirs ()) return 99;
    initDico ();
    test ();

    // file to open : ??? in args, first that exists

    QStringList arguments (QApplication::arguments ());
    QString     documentPath;

    for (int index = 1; index < arguments.size (); index++)
    {
        const QString & arg (arguments.at (index));
        if (QFile (arg).exists ())
        {
            documentPath = arg;
            break;
        }
    }

    // window and application execution

    MainWindow window (documentPath);
    splash.finish (&window);
    window.show ();

    return (app.exec ());
}

//}}} --------------------------------------------------------------------------

